package com.shell.governor.modules.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.entity.auth.AuthUser;
import com.shell.bee.entity.mvc.RetMsg;
import com.shell.bee.persistence.annotation.Pagination;
import com.shell.bee.persistence.model.PageLimit;
import com.shell.bee.persistence.service.impl.PersistentServiceImpl;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.framework.utils.SysDateUtil;
import com.shell.governor.authorization.service.PubRoleService;
import com.shell.governor.modules.dao.PubMessageDao;
import com.shell.governor.modules.dao.PubMessageReceiverDao;
import com.shell.governor.modules.po.PubMessage;
import com.shell.governor.modules.service.PubMessageService;
import com.shell.governor.user.po.PubUser;
import com.shell.governor.user.service.PubUserService;

import tk.mybatis.mapper.entity.Example;

@Service
public class PubMessageServiceImpl extends PersistentServiceImpl<PubMessage> implements PubMessageService {
	@Autowired
	protected PubMessageDao pubMessageDao;
	@Autowired
	protected PubRoleService pubRoleService;
	@Autowired
	private PubUserService pubUserService;
	@Autowired
	private PubMessageReceiverDao messageReceiverDao;

	public RetMsg setPubMessageReaded(List<String> messageIds) {
		int update_readed_count = this.pubMessageDao.setPubMessageReaded(messageIds, SysDateUtil.getSystemDateTime());
		if (update_readed_count > 0) {
			return new RetMsg("success", false, "������������");
		}
		return new RetMsg("error", false, "��������������");
	}

	@Pagination
	public List<PubMessage> getMyMessagesByPage(PageLimit limit, PubMessage message) {
		Example example = new Example(PubMessage.class);
		Example.Criteria criteria = example.createCriteria();
		if (StringUtil.notTrimEmpty(message.getMessageTypeCode())) {
			if ("readed".equals(message.getMessageTypeCode())) {
				criteria.andIsNotNull("readTime");
			} else if ("notReaded".equals(message.getMessageTypeCode())) {
				criteria.andIsNull("readTime");
			}
		}
		if (StringUtils.isNotEmpty(message.getContent())) {
			criteria.andLike("content", StringUtil.getParameterLike(message.getContent()));
		}
		if (StringUtils.isNotEmpty(message.getTitle())) {
			criteria.andLike("title", StringUtil.getParameterLike(message.getTitle()));
		}
		String receiverId = "";
		if (StringUtil.emptyAndNull(message.getReceiverId())) {
			receiverId = CommonDataUtil.getUserId();
		} else {
			receiverId = message.getReceiverId();
		}
		criteria.andEqualTo("receiverId", receiverId);

		example.setOrderByClause("send_time DESC");
		List<PubMessage> messages = this.pubMessageDao.selectByExample(example);
		return messages;
	}

	public Long getMyMessageNotReadedCount() {
		PubMessage param = new PubMessage();

		AuthUser user = CommonDataUtil.getCurUser();
		param.setReceiverId(user.getUserId());

		Long notReadedCount = Long.valueOf(this.pubMessageDao.getPubMessageNotReadedCount(param));
		return notReadedCount;
	}

	public List<String> getReceviers(String[] roles, String organId) {
		List<String> ret = new ArrayList();
		List<PubUser> userList = this.pubUserService.selectUsersByRoleCodes(organId, roles);
		for (PubUser user : userList) {
			ret.add(user.getUserId());
		}
		return ret;
	}

	public List<String> getReceviersByTemplateId(String messageTemplateId, String entId) {
		return this.messageReceiverDao.getReceiver(messageTemplateId, entId);
	}
}
